package com.yiquan.comment.dao;


import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

import com.yiquan.comment.pojo.Comment;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

/**
 * 数据访问接口
 * @author Administrator
 *
 */
public interface CommentDao extends JpaRepository<Comment,Long>,JpaSpecificationExecutor<Comment>{


    /**
     * 根据id修改评论状态(删除评论)
     * @param id
     */
    @Query(value="update yiquan_comment set comment_type = 1 where id = ?1",nativeQuery=true)
    @Modifying
    @Transactional
    public void updateCommentTypeById(Long id);


    /**
     * 查询某条视频或某条评论下的评论总数
     * @param parent_id
     * @param parent_type
     * @return
     */
    @Query(value="select count(*) from yiquan_comment where parent_id=?1 and parent_type=?2 and comment_type=0",nativeQuery=true)
    Long findCount(Object parent_id, Object parent_type);

    /**
     * 查询视频下所有子评论总数
     * @param parent_id
     * @param parent_type
     * @return
     */
    @Query(value="select count(*) from yiquan_comment where comment_type = 0 and parent_type = 1 and parent_id in (select id from yiquan_comment where parent_id = ?1 and parent_type = ?2 and comment_type = 0)",nativeQuery=true)
    Long findChildrenCount(Object parent_id, Object parent_type);

    /**
     * 通过父id查询评论
     * @param parentId
     * @return
     */
    @Query(value="select * from yiquan_comment where parent_id = ?1",nativeQuery=true)
    Comment findByParentId(Long parentId);

    /**
     * 查询某条视频评论的子评论数
     * @param parentId
     * @return
     */
    @Query(value="select count(*) from yiquan_comment where parent_id = ?1 and parent_type = 1 and comment_type = 0",nativeQuery=true)
    Integer findCountByParentId(Long parentId);

    @Query(value = "select count(*) from yiquan_comment where  top_comment_id = ?1",nativeQuery = true)
    Integer countChildCommentByTopId(Long topCommentId);
}
